//
//  LKImageScrollViewController.m
//  JingWaiYou
//
//  Created by zzkai.liu on 14/12/20.
//  Copyright (c) 2014年 ZhunNa. All rights reserved.
//

#import "LKImageScrollViewController.h"

#import "UIImageView+AFNetworking.h"

@interface LKImageScrollViewController ()<UIScrollViewDelegate>
{
    UIScrollView *imgScrollView;
    UILabel *_tittleLabel;
    float _midX;
    float _midY;
}

@end

@implementation LKImageScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    NSString *tittleStr = [NSString stringWithFormat:@"%ld/%lu",self.index + 1,(unsigned long)self.imgUrlArr.count];
    //[navigationBar setTitle:tittleStr];
    _tittleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,VIEW_HEIGHT - 60, VIEW_WIDTH, 44)];
    _tittleLabel.textColor = [UIColor whiteColor];
    _tittleLabel.font = [UIFont fontWithName:@"YaHei" size:17];
    _tittleLabel.textAlignment = NSTextAlignmentCenter;
    _tittleLabel.text = tittleStr;
    [self.view addSubview:_tittleLabel];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    imgScrollView.userInteractionEnabled = YES;
    imgScrollView.pagingEnabled = YES;
    imgScrollView.contentSize = CGSizeMake(VIEW_WIDTH * self.imgUrlArr.count, 0);
    imgScrollView.showsHorizontalScrollIndicator = NO;
    imgScrollView.showsVerticalScrollIndicator = NO;
    imgScrollView.scrollsToTop = NO;
    imgScrollView.bounces = NO;
    imgScrollView.delegate = self;
    imgScrollView.tag = 1001;
    for (int i = 0; i < self.imgUrlArr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, imgScrollView.frame.size.height)];
        UIScrollView *subScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * VIEW_WIDTH, 0, VIEW_WIDTH, imgScrollView.frame.size.height)];
        subScroll.multipleTouchEnabled = YES;
        subScroll.contentSize = imgView.frame.size;
        subScroll.showsHorizontalScrollIndicator = NO;
        subScroll.showsVerticalScrollIndicator = NO;
        subScroll.maximumZoomScale = 2.0f;
        subScroll.minimumZoomScale = 1.0f;
        subScroll.delegate = self;
        subScroll.bounces = NO;
        subScroll.directionalLockEnabled = YES;
        subScroll.alwaysBounceVertical = NO;
        subScroll.tag = i + 200;
        imgView.tag = i + 100;
        [subScroll addSubview:imgView];
        [imgScrollView addSubview:subScroll];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
        [singleTap setNumberOfTapsRequired:1];
        [singleTap setNumberOfTouchesRequired:1];
        [subScroll addGestureRecognizer:singleTap];
        UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
        [doubleTapGestureRecognizer setNumberOfTapsRequired:2];
        [subScroll addGestureRecognizer:doubleTapGestureRecognizer];
        [singleTap requireGestureRecognizerToFail:doubleTapGestureRecognizer];
    }
    [self.view addSubview:imgScrollView];

    [imgScrollView setContentOffset:CGPointMake(self.index * VIEW_WIDTH, 0) animated:NO];
    if (self.index == 0) {
        [self loadImage];
    }
    [self.view bringSubviewToFront:_tittleLabel];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void) doubleTap:(UITapGestureRecognizer*)tap
{
    UIScrollView *tapView = (UIScrollView*)tap.view;
    if (tapView.zoomScale == 1) {
        [tapView setZoomScale:2 animated:YES];
    }else
    {
        [tapView setZoomScale:1 animated:YES];
    }
}

- (void) loadImage
{
    //NSLog(@"%d",self.index);
    UIScrollView *subView = (UIScrollView*)[imgScrollView viewWithTag:self.index + 200];
    UIImageView *imgView = (UIImageView*)[subView viewWithTag:self.index + 100];
    
    for (id obj in self.imgUrlArr) {
        if ([obj isKindOfClass:[UIImage class]]) {
            imgView.image = obj;
            imgView.contentMode = UIViewContentModeScaleAspectFit;
            subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y, imgView.frame.size.width, imgView.frame.size.height);
            return;
        }else{
            break;
        }
    }
    if (imgView.image == nil) {
        [imgView setImageWithURL:[NSURL URLWithString:[self.imgUrlArr objectAtIndex:self.index]] placeholderImage:[UIImage imageNamed:@"ssehLogo"]];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y, imgView.frame.size.width, imgView.frame.size.height);
    }
}

- (void)back{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self back];
}
#pragma -mark ScrollViewDelegate
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag == 1001) {
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.index = page;
        NSString *tittleStr = [NSString stringWithFormat:@"%ld/%lu",self.index + 1,(unsigned long)self.imgUrlArr.count];
        _tittleLabel.text = tittleStr;
        [self loadImage];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 1001) {
        UIScrollView *subView1 = (UIScrollView*)[imgScrollView viewWithTag:self.index + 200];
        [subView1 setZoomScale:1 animated:YES];
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIImageView *imgView = (UIImageView*)[imgScrollView viewWithTag:scrollView.tag - 100];
    
    return imgView;
}

- (void) scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    UIImageView *imgView = (UIImageView*)[imgScrollView viewWithTag:scrollView.tag - 100];
    imgView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
